package edu.wschina.module_c_hanoitower;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
    private List<JSONObject> items;
    private AdapterView.OnItemClickListener listener;
    // 构造函数
    public CustomAdapter(List<JSONObject> items) {
        this.items = items;
    }

    // 创建新的 ViewHolder
    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.rank_item, parent, false);
        return new CustomViewHolder(itemView);
    }

    // 绑定数据到 ViewHolder
    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        try {
            JSONObject item = items.get(position);
            holder.id.setText(item.getString("id"));
            holder.name.setText(item.getString("nickname"));
            holder.move.setText(item.getString("moves"));
            holder.diff.setText(item.getString("difference"));
            holder.duration.setText(item.getString("duration"));
        }catch (Exception e){

        }
//        holder.date.setText(item.date);
    }

    // 返回数据集大小
    @Override
    public int getItemCount() {
        return items.size();
    }

    public void clearItems(){
        items=new ArrayList<>();
    }

    public void addItems(List<JSONObject> newItems) {
        items.addAll(newItems);
        notifyDataSetChanged();
    }

    /// 提供对视图的引用（自定义的ViewHolder）
    public static class CustomViewHolder extends RecyclerView.ViewHolder {
        // 声明你的视图组件引用

        public TextView id;
        public TextView name;
        public TextView duration;
        public TextView move;
        public TextView diff;
        public TextView date;

        public CustomViewHolder(View itemView) {
            super(itemView);

            id=itemView.findViewById(R.id.id);
            name = itemView.findViewById(R.id.name);
            duration=itemView.findViewById(R.id.duration);
            move=itemView.findViewById(R.id.moves);
            diff=itemView.findViewById(R.id.difference);
//            date=itemView.findViewById(R.id.date);
        }
    }
}
